/****************************************************************************************************************************
 * @File name:elec_sw.c
 * @Author:sunshu
 * @Version:v2.00
 * @Date:2017.12/09
 * @Description:control the states of the electric switchs through MCU pin directly
 * @Others:
 * @Function List:
	 1.void AmuxEnableAll(void):Enable all mux ICs enable pin
	 2.void AmuxDisableAll(void):Disable all mux ICs enable pin
	 3.void U3_1_Channel(unsigned char sel):control the states of the ADG1408 including U3_1A,U3_1B,U3_1C and U3_2A,U3_2B,U3_2C
	 4.void U4_1_Channel(uint8_t sel):control the states of the ADG1408 including U4_1
	 5.unsigned char VoltageEnable(unsigned char channel):control the states of the ADG1408 including U3_1A,U3_1B,U3_1C,U3_2A,U3_2B,U3_2C and U4_1.
	 6.void U3_3_Channel(unsigned char sel):control the states of the ADG1408 including U3_3A,U3_3B,U3_3C and  U3_4A,U3_4B,U3_4C
	 7.void U4_2_Channel(unsigned char sel):control the states of the ADG1408 including U4_2
	 8.unsigned char ResistanceEnable(unsigned char channel):control the states of the ADG1408 including U3_3A,U3_3B,U3_3C,U3_4A,U3_4B,U3_4C and U4_2.
	 9.void U5_1_Channel(unsigned char sel):control the states of the ADG849(U5_1) and ADG1409(U5_2)
	 10.unsigned char MeasuringSwitch(unsigned char channel):control the states of the ADG849(U5_1) and ADG1409(U5_2)
 * @History:
****************************************************************************************************************************/

#include "stm32f4xx_hal.h"
#include "elec_sw.h"
#include "delay.h"

unsigned char ExtAdcChannel1(unsigned char channel);
unsigned char ExtAdcChannel2(unsigned char channel);


ExtAdc_interface UserExtPort = {
    ExtAdcChannel1,
    ExtAdcChannel2,
};

/*-----------------Define AMUX IC EN pin set --------------------*/
#define SW_EN_ALL_HIGH()		HAL_GPIO_WritePin(GPIOB, EN_H1_Pin|EN_H2_Pin, GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC, EN_H3_Pin, GPIO_PIN_SET)
#define	SW_EN_ALL_LOW()			HAL_GPIO_WritePin(GPIOB, EN_H1_Pin|EN_H2_Pin, GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOC, EN_H3_Pin, GPIO_PIN_RESET)

/*-----------------Define AMUX IC EN pin set --------------------*/
#define U3_1A0_HIGH()			HAL_GPIO_WritePin(ADG1408_U3_1or2ABC_A0_GPIO_Port, ADG1408_U3_1or2ABC_A0_Pin, GPIO_PIN_SET)
#define U3_1A0_LOW()			HAL_GPIO_WritePin(ADG1408_U3_1or2ABC_A0_GPIO_Port, ADG1408_U3_1or2ABC_A0_Pin, GPIO_PIN_RESET)

#define U3_1A1_HIGH()			HAL_GPIO_WritePin(ADG1408_U3_1or2ABC_A1_GPIO_Port, ADG1408_U3_1or2ABC_A1_Pin, GPIO_PIN_SET)
#define U3_1A1_LOW()			HAL_GPIO_WritePin(ADG1408_U3_1or2ABC_A1_GPIO_Port, ADG1408_U3_1or2ABC_A1_Pin, GPIO_PIN_RESET)

#define U3_1A2_HIGH()			HAL_GPIO_WritePin(ADG1408_U3_1or2ABC_A2_GPIO_Port, ADG1408_U3_1or2ABC_A2_Pin, GPIO_PIN_SET)
#define U3_1A2_LOW()			HAL_GPIO_WritePin(ADG1408_U3_1or2ABC_A2_GPIO_Port, ADG1408_U3_1or2ABC_A2_Pin, GPIO_PIN_RESET)

#define U4_1A0_HIGH()			HAL_GPIO_WritePin(ADG1408_U4_1_A0_GPIO_Port, ADG1408_U4_1_A0_Pin, GPIO_PIN_SET)
#define U4_1A0_LOW()			HAL_GPIO_WritePin(ADG1408_U4_1_A0_GPIO_Port, ADG1408_U4_1_A0_Pin, GPIO_PIN_RESET)

#define U4_1A1_HIGH()			HAL_GPIO_WritePin(ADG1408_U4_1_A1_GPIO_Port, ADG1408_U4_1_A1_Pin, GPIO_PIN_SET)
#define U4_1A1_LOW()			HAL_GPIO_WritePin(ADG1408_U4_1_A1_GPIO_Port, ADG1408_U4_1_A1_Pin, GPIO_PIN_RESET)

#define U4_1A2_HIGH()			HAL_GPIO_WritePin(ADG1408_U4_1_A2_GPIO_Port, ADG1408_U4_1_A2_Pin, GPIO_PIN_SET)
#define U4_1A2_LOW()			HAL_GPIO_WritePin(ADG1408_U4_1_A2_GPIO_Port, ADG1408_U4_1_A2_Pin, GPIO_PIN_RESET)

#define DISCHG1_SWITCH__HIGH()		HAL_GPIO_WritePin(DISCHG_SWCTL1_GPIO_Port, DISCHG_SWCTL1_Pin, GPIO_PIN_SET)
#define DISCHG1_SWITCH__LOW()			HAL_GPIO_WritePin(DISCHG_SWCTL1_GPIO_Port, DISCHG_SWCTL1_Pin, GPIO_PIN_RESET)

#define DISCHG2_SWITCH__HIGH()		HAL_GPIO_WritePin(DISCHG_SWCTL2_GPIO_Port, DISCHG_SWCTL2_Pin, GPIO_PIN_SET)
#define DISCHG2_SWITCH__LOW()			HAL_GPIO_WritePin(DISCHG_SWCTL2_GPIO_Port, DISCHG_SWCTL2_Pin, GPIO_PIN_RESET)


/****************************************************************************************************************************
* @Functions:AmuxEnableAll()
* @Description:Enable all mux ICs enable pin
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void AmuxEnableAll(void)
{
    SW_EN_ALL_HIGH();
}

/****************************************************************************************************************************
* @Functions:AmuxDisableAll()
* @Description:Disable all mux ICs enable pin
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void AmuxDisableAll(void)
{
    SW_EN_ALL_LOW();
}


/****************************************************************************************************************************
* @Functions:U3_1_Channel()
* @Description:control the switch Rail-to-Rail of the ADG1408 including U3_1A,U3_1B,U3_1C and  U3_2A,U3_2B,U3_2C. The ADG1408
	switches one of eight inputs to a common output as determined by the 3-bit binary address lines A0,A1 and A2.
* @Input:one of eight inputs
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void U3_1_Channel(unsigned char sel)
{
    switch(sel)
    {
    case 0:
        U3_1A2_LOW();
        U3_1A1_LOW();
        U3_1A0_LOW();
        break;

    case 1:
        U3_1A2_LOW();
        U3_1A1_LOW();
        U3_1A0_HIGH();
        break;

    case 2:
        U3_1A2_LOW();
        U3_1A1_HIGH();
        U3_1A0_LOW();
        break;

    case 3:
        U3_1A2_LOW();
        U3_1A1_HIGH();
        U3_1A0_HIGH();
        break;

    case 4:
        U3_1A2_HIGH();
        U3_1A1_LOW();
        U3_1A0_LOW();
        break;

    case 5:
        U3_1A2_HIGH();
        U3_1A1_LOW();
        U3_1A0_HIGH();
        break;

    case 6:
        U3_1A2_HIGH();
        U3_1A1_HIGH();
        U3_1A0_LOW();
        break;

    case 7:
        U3_1A2_HIGH();
        U3_1A1_HIGH();
        U3_1A0_HIGH();
        break;
    }
}


/****************************************************************************************************************************
* @Functions:U4_1_Channel()
* @Description:control the switch Rail-to-Rail of the ADG1408 including U4_1. The ADG1408 switches one of eight inputs to a
	common output as determined by the 3-bit binary address lines A0,A1 and A2.
* @Input:one of eight inputs
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void U4_1_Channel(uint8_t sel)
{
    switch(sel)
    {
    case 0:
        U4_1A2_LOW();
        U4_1A1_LOW();
        U4_1A0_LOW();
        break;

    case 1:
        U4_1A2_LOW();
        U4_1A1_LOW();
        U4_1A0_HIGH();
        break;

    case 2:
        U4_1A2_LOW();
        U4_1A1_HIGH();
        U4_1A0_LOW();
        break;

    case 3:
        U4_1A2_LOW();
        U4_1A1_HIGH();
        U4_1A0_HIGH();
        break;

    case 4:
        U4_1A2_HIGH();
        U4_1A1_LOW();
        U4_1A0_LOW();
        break;

    case 5:
        U4_1A2_HIGH();
        U4_1A1_LOW();
        U4_1A0_HIGH();
        break;
    case 6:
        U4_1A2_HIGH();
        U4_1A1_HIGH();
        U4_1A0_LOW();
        break;
    case 7:
        U4_1A2_HIGH();
        U4_1A1_HIGH();
        U4_1A0_HIGH();
        break;
    }
}


/****************************************************************************************************************************
* @Functions:VoltageEnable()
* @Description:control the states of the electric switch including U3_1A,U3_1B,U3_1C,U3_2A,U3_2B,U3_2C and U4_1.
* @Input:channel:0~47
* @Output:
* @Return:insignificance
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char VoltageEnable(unsigned char channel)
{
    if(channel > 47)		// Switch to NC
    {
        U3_1_Channel(0);
        Delay_us(2);
        U4_1_Channel(7);	// AMUX NC pin
        Delay_us(2);
        return 0;
    }
    U3_1_Channel(channel%8);
    Delay_us(2);
    U4_1_Channel(channel/8);
    Delay_us(2);

    return 1;
}

//============================================================================
#define U3_3A0_HIGH()			HAL_GPIO_WritePin(ADG1408_U3_3or4ABC_A0_GPIO_Port, ADG1408_U3_3or4ABC_A0_Pin, GPIO_PIN_SET)
#define U3_3A0_LOW()			HAL_GPIO_WritePin(ADG1408_U3_3or4ABC_A0_GPIO_Port, ADG1408_U3_3or4ABC_A0_Pin, GPIO_PIN_RESET)

#define U3_3A1_HIGH()			HAL_GPIO_WritePin(ADG1408_U3_3or4ABC_A1_GPIO_Port, ADG1408_U3_3or4ABC_A1_Pin, GPIO_PIN_SET)
#define U3_3A1_LOW()			HAL_GPIO_WritePin(ADG1408_U3_3or4ABC_A1_GPIO_Port, ADG1408_U3_3or4ABC_A1_Pin, GPIO_PIN_RESET)

#define U3_3A2_HIGH()			HAL_GPIO_WritePin(ADG1408_U3_3or4ABC_A2_GPIO_Port, ADG1408_U3_3or4ABC_A2_Pin, GPIO_PIN_SET)
#define U3_3A2_LOW()			HAL_GPIO_WritePin(ADG1408_U3_3or4ABC_A2_GPIO_Port, ADG1408_U3_3or4ABC_A2_Pin, GPIO_PIN_RESET)

#define U4_2A0_HIGH()			HAL_GPIO_WritePin(ADG1408_U4_2_A0_GPIO_Port, ADG1408_U4_2_A0_Pin, GPIO_PIN_SET)
#define U4_2A0_LOW()			HAL_GPIO_WritePin(ADG1408_U4_2_A0_GPIO_Port, ADG1408_U4_2_A0_Pin, GPIO_PIN_RESET)

#define U4_2A1_HIGH()			HAL_GPIO_WritePin(ADG1408_U4_2_A1_GPIO_Port, ADG1408_U4_2_A1_Pin, GPIO_PIN_SET)
#define U4_2A1_LOW()			HAL_GPIO_WritePin(ADG1408_U4_2_A1_GPIO_Port, ADG1408_U4_2_A1_Pin, GPIO_PIN_RESET)

#define U4_2A2_HIGH()			HAL_GPIO_WritePin(ADG1408_U4_2_A2_GPIO_Port, ADG1408_U4_2_A2_Pin, GPIO_PIN_SET)
#define U4_2A2_LOW()			HAL_GPIO_WritePin(ADG1408_U4_2_A2_GPIO_Port, ADG1408_U4_2_A2_Pin, GPIO_PIN_RESET)


/****************************************************************************************************************************
* @Functions:U3_3_Channel()
* @Description:control the switch Rail-to-Rail of the ADG1408 including U3_3A,U3_3B,U3_3C and  U3_4A,U3_4B,U3_4C. The ADG1408
	switches one of eight inputs to a common output as determined by the 3-bit binary address lines A0,A1 and A2.
* @Input:one of eight inputs
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void U3_3_Channel(unsigned char sel)
{
    switch(sel)
    {
    case 0:
        U3_3A2_LOW();
        U3_3A1_LOW();
        U3_3A0_LOW();
        break;

    case 1:
        U3_3A2_LOW();
        U3_3A1_LOW();
        U3_3A0_HIGH();
        break;

    case 2:
        U3_3A2_LOW();
        U3_3A1_HIGH();
        U3_3A0_LOW();
        break;

    case 3:
        U3_3A2_LOW();
        U3_3A1_HIGH();
        U3_3A0_HIGH();
        break;

    case 4:
        U3_3A2_HIGH();
        U3_3A1_LOW();
        U3_3A0_LOW();
        break;

    case 5:
        U3_3A2_HIGH();
        U3_3A1_LOW();
        U3_3A0_HIGH();
        break;

    case 6:
        U3_3A2_HIGH();
        U3_3A1_HIGH();
        U3_3A0_LOW();
        break;

    case 7:
        U3_3A2_HIGH();
        U3_3A1_HIGH();
        U3_3A0_HIGH();
        break;
    }
}

/****************************************************************************************************************************
* @Functions:U4_2_Channel()
* @Description:control the switch Rail-to-Rail of the ADG1408 including U4_2. The ADG1408 switches one of eight inputs to a
	common output as determined by the 3-bit binary address lines A0,A1 and A2.
* @Input:one of eight inputs
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void U4_2_Channel(unsigned char sel)
{
    switch(sel)
    {
    case 0:
        U4_2A2_LOW();
        U4_2A1_LOW();
        U4_2A0_LOW();
        break;

    case 1:
        U4_2A2_LOW();
        U4_2A1_LOW();
        U4_2A0_HIGH();
        break;

    case 2:
        U4_2A2_LOW();
        U4_2A1_HIGH();
        U4_2A0_LOW();
        break;

    case 3:
        U4_2A2_LOW();
        U4_2A1_HIGH();
        U4_2A0_HIGH();
        break;

    case 4:
        U4_2A2_HIGH();
        U4_2A1_LOW();
        U4_2A0_LOW();
        break;

    case 5:
        U4_2A2_HIGH();
        U4_2A1_LOW();
        U4_2A0_HIGH();
        break;

    case 6:
        U4_2A2_HIGH();
        U4_2A1_HIGH();
        U4_2A0_LOW();
        break;

    case 7:
        U4_2A2_HIGH();
        U4_2A1_HIGH();
        U4_2A0_HIGH();
        break;

    default:
        U4_2A2_HIGH();
        U4_2A1_HIGH();
        U4_2A0_HIGH();
    }
}


/****************************************************************************************************************************
* @Functions:ResistanceEnable()
* @Description:control the states of the electric switch including U3_3A,U3_3B,U3_3C,U3_4A,U3_4B,U3_4C and U4_2.
* @Input:channel:0~47
* @Output:
* @Return:insignificance
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char ResistanceEnable(unsigned char channel)
{
    if(channel > 47)		// Switch to NC
    {
        U3_3_Channel(0);
        Delay_us(2);
        U4_2_Channel(7);	// AMUX NC pin
        Delay_us(2);
        return 0;
    }
    U3_3_Channel(channel%8);
    Delay_us(2);
    U4_2_Channel(channel/8);
    Delay_us(2);
    return 1;
}



//==================================================
#define	U5_1A0_HIGH()			HAL_GPIO_WritePin(ADG1409_U5_2_A0_GPIO_Port, ADG1409_U5_2_A0_Pin, GPIO_PIN_SET)
#define	U5_1A0_LOW()			HAL_GPIO_WritePin(ADG1409_U5_2_A0_GPIO_Port, ADG1409_U5_2_A0_Pin, GPIO_PIN_RESET)

#define	U5_1A1_HIGH()			HAL_GPIO_WritePin(ADG1409_U5_2_A1_GPIO_Port, ADG1409_U5_2_A1_Pin, GPIO_PIN_SET)
#define	U5_1A1_LOW()			HAL_GPIO_WritePin(ADG1409_U5_2_A1_GPIO_Port, ADG1409_U5_2_A1_Pin, GPIO_PIN_RESET)

#define	U5_1A2_HIGH()			HAL_GPIO_WritePin(ADG619_U5_1_GPIO_Port, ADG619_U5_1_Pin, GPIO_PIN_SET)
#define	U5_1A2_LOW()			HAL_GPIO_WritePin(ADG619_U5_1_GPIO_Port, ADG619_U5_1_Pin, GPIO_PIN_RESET)


/****************************************************************************************************************************
* @Functions:U5_1_Channel()
* @Description:control the switch Rail-to-Rail of the ADG849(U5_1) and ADG1409(U5_2).
* @Input:0~3:voltage through dividing resistors,4~7:constant flow source
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void U5_1_Channel(unsigned char sel)
{
    switch(sel)
    {
    case 0:
        U5_1A2_LOW();														//the input of ADG619
        U5_1A1_LOW();														//ADG1409 A1
        U5_1A0_LOW();														//ADG1409 A0
        break;

    case 1:
        U5_1A2_LOW();
        U5_1A1_LOW();
        U5_1A0_HIGH();
        break;

    case 2:
        U5_1A2_LOW();
        U5_1A1_HIGH();
        U5_1A0_LOW();
        break;

    case 3:
        U5_1A2_LOW();
        U5_1A1_HIGH();
        U5_1A0_HIGH();
        break;

    case 4:
        U5_1A2_HIGH();
        U5_1A1_LOW();
        U5_1A0_LOW();
        break;

    case 5:
        U5_1A2_HIGH();
        U5_1A1_LOW();
        U5_1A0_HIGH();
        break;

    case 6:
        U5_1A2_HIGH();
        U5_1A1_HIGH();
        U5_1A0_LOW();
        break;

    case 7:
        U5_1A2_HIGH();
        U5_1A1_HIGH();
        U5_1A0_HIGH();
        break;

    default:
        U5_1A2_HIGH();
        U5_1A1_HIGH();
        U5_1A0_HIGH();
        break;
    }
}


/****************************************************************************************************************************
* @Functions:MeasuringSwitch()
* @Description:control the switch Rail-to-Rail of the ADG849(U5_1) and ADG1409(U5_2).
* @Input:0~3:voltage through dividing resistors(10k, 100k, 1M, 10M),4~7:constant flow source(cap and 100, 1000, 1k)
* @Output:
* @Return:insignificance
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char MeasuringSwitch(unsigned char channel)         //0~3:DDS BUF  4~6:cap or res(below 1k)  7:res(above 1k)
{
    U5_1_Channel(channel);
    Delay_us(2);
    return 1;
}

void PP4V37_EN()
{
//	HAL_GPIO_WritePin(Switch_DC_AC_GPIO_Port, Switch_DC_AC_Pin, GPIO_PIN_SET);
    HAL_GPIO_WritePin(ADG849_REF_SWCTL1_GPIO_Port, ADG849_REF_SWCTL1_Pin, GPIO_PIN_SET);
}

void PP2V5_EN()
{
//	HAL_GPIO_WritePin(Switch_DC_AC_GPIO_Port, Switch_DC_AC_Pin, GPIO_PIN_SET);
    HAL_GPIO_WritePin(ADG849_REF_SWCTL1_GPIO_Port, ADG849_REF_SWCTL1_Pin, GPIO_PIN_RESET);
}


#define	U2_1A0_HIGH()			HAL_GPIO_WritePin(ADG1408_U2_1or3ABC_A0_GPIO_Port, ADG1408_U2_1or3ABC_A0_Pin, GPIO_PIN_SET)
#define	U2_1A0_LOW()			HAL_GPIO_WritePin(ADG1408_U2_1or3ABC_A0_GPIO_Port, ADG1408_U2_1or3ABC_A0_Pin, GPIO_PIN_RESET)

#define	U2_1A1_HIGH()			HAL_GPIO_WritePin(ADG1408_U2_1or3ABC_A1_GPIO_Port, ADG1408_U2_1or3ABC_A1_Pin, GPIO_PIN_SET)
#define	U2_1A1_LOW()			HAL_GPIO_WritePin(ADG1408_U2_1or3ABC_A1_GPIO_Port, ADG1408_U2_1or3ABC_A1_Pin, GPIO_PIN_RESET)

#define	U2_1A2_HIGH()			HAL_GPIO_WritePin(ADG1408_U2_1or3ABC_A2_GPIO_Port, ADG1408_U2_1or3ABC_A2_Pin, GPIO_PIN_SET)
#define	U2_1A2_LOW()			HAL_GPIO_WritePin(ADG1408_U2_1or3ABC_A2_GPIO_Port, ADG1408_U2_1or3ABC_A2_Pin, GPIO_PIN_RESET)

/****************************************************************************************************************************
* @Functions:U2_1_Channel()
* @Description:control the states the ADG1408 including U2_1A,U2_1B,U2_1C and  U2_3A,U2_3B,U2_3C. The ADG1408switches one of
	eight inputs to a common output as determined by the 3-bit binary address lines A0,A1 and A2.
* @Input:one of eight inputs
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void U2_1_Channel(unsigned char sel)
{
    switch(sel)
    {
    case 0:
        U2_1A2_LOW();
        U2_1A1_LOW();
        U2_1A0_LOW();
        break;

    case 1:
        U2_1A2_LOW();
        U2_1A1_LOW();
        U2_1A0_HIGH();
        break;

    case 2:
        U2_1A2_LOW();
        U2_1A1_HIGH();
        U2_1A0_LOW();
        break;

    case 3:
        U2_1A2_LOW();
        U2_1A1_HIGH();
        U2_1A0_HIGH();
        break;

    case 4:
        U2_1A2_HIGH();
        U2_1A1_LOW();
        U2_1A0_LOW();
        break;

    case 5:
        U2_1A2_HIGH();
        U2_1A1_LOW();
        U2_1A0_HIGH();
        break;

    case 6:
        U2_1A2_HIGH();
        U2_1A1_HIGH();
        U2_1A0_LOW();
        break;

    case 7:
        U2_1A2_HIGH();
        U2_1A1_HIGH();
        U2_1A0_HIGH();
        break;
    }
}


#define	U2_2A0_HIGH()			HAL_GPIO_WritePin(ADG1408_U2_2or4ABC_A0_GPIO_Port, ADG1408_U2_2or4ABC_A0_Pin, GPIO_PIN_SET)
#define	U2_2A0_LOW()			HAL_GPIO_WritePin(ADG1408_U2_2or4ABC_A0_GPIO_Port, ADG1408_U2_2or4ABC_A0_Pin, GPIO_PIN_RESET)

#define	U2_2A1_HIGH()			HAL_GPIO_WritePin(ADG1408_U2_2or4ABC_A1_GPIO_Port, ADG1408_U2_2or4ABC_A1_Pin, GPIO_PIN_SET)
#define	U2_2A1_LOW()			HAL_GPIO_WritePin(ADG1408_U2_2or4ABC_A1_GPIO_Port, ADG1408_U2_2or4ABC_A1_Pin, GPIO_PIN_RESET)

#define	U2_2A2_HIGH()			HAL_GPIO_WritePin(ADG1408_U2_2or4ABC_A2_GPIO_Port, ADG1408_U2_2or4ABC_A2_Pin, GPIO_PIN_SET)
#define	U2_2A2_LOW()			HAL_GPIO_WritePin(ADG1408_U2_2or4ABC_A2_GPIO_Port, ADG1408_U2_2or4ABC_A2_Pin, GPIO_PIN_RESET)

/****************************************************************************************************************************
* @Functions:U2_2_Channel()
* @Description:control the states the ADG1408 including U2_2A,U2_2B,U2_2C and  U2_4A,U2_4B,U2_4C. The ADG1408switches one of
	eight inputs to a common output as determined by the 3-bit binary address lines A0,A1 and A2.
* @Input:one of eight inputs
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void U2_2_Channel(unsigned char sel)
{
    switch(sel)
    {
    case 0:
        U2_2A2_LOW();
        U2_2A1_LOW();
        U2_2A0_LOW();
        break;

    case 1:
        U2_2A2_LOW();
        U2_2A1_LOW();
        U2_2A0_HIGH();
        break;

    case 2:
        U2_2A2_LOW();
        U2_2A1_HIGH();
        U2_2A0_LOW();
        break;

    case 3:
        U2_2A2_LOW();
        U2_2A1_HIGH();
        U2_2A0_HIGH();
        break;

    case 4:
        U2_2A2_HIGH();
        U2_2A1_LOW();
        U2_2A0_LOW();
        break;

    case 5:
        U2_2A2_HIGH();
        U2_2A1_LOW();
        U2_2A0_HIGH();
        break;

    case 6:
        U2_2A2_HIGH();
        U2_2A1_HIGH();
        U2_2A0_LOW();
        break;

    case 7:
        U2_2A2_HIGH();
        U2_2A1_HIGH();
        U2_2A0_HIGH();
        break;
    }
}

#define	U1_1A0_HIGH()			HAL_GPIO_WritePin(ADG1408_U1_1_A0_GPIO_Port, ADG1408_U1_1_A0_Pin, GPIO_PIN_SET)
#define	U1_1A0_LOW()			HAL_GPIO_WritePin(ADG1408_U1_1_A0_GPIO_Port, ADG1408_U1_1_A0_Pin, GPIO_PIN_RESET)

#define	U1_1A1_HIGH()			HAL_GPIO_WritePin(ADG1408_U1_1_A1_GPIO_Port, ADG1408_U1_1_A1_Pin, GPIO_PIN_SET)
#define	U1_1A1_LOW()			HAL_GPIO_WritePin(ADG1408_U1_1_A1_GPIO_Port, ADG1408_U1_1_A1_Pin, GPIO_PIN_RESET)

#define	U1_1A2_HIGH()			HAL_GPIO_WritePin(ADG1408_U1_1_A2_GPIO_Port, ADG1408_U1_1_A2_Pin, GPIO_PIN_SET)
#define	U1_1A2_LOW()			HAL_GPIO_WritePin(ADG1408_U1_1_A2_GPIO_Port, ADG1408_U1_1_A2_Pin, GPIO_PIN_RESET)


/****************************************************************************************************************************
* @Functions:U1_1_Channel()
* @Description:control the states the ADG1408 including U1_1. The ADG1408switches one of eight inputs to a common output as
	determined by the 3-bit binary address lines A0,A1 and A2.
* @Input:one of 6 inputs
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void U1_1_Channel(unsigned char sel)
{
    switch(sel)
    {
    case 0:
        U1_1A2_LOW();
        U1_1A1_LOW();
        U1_1A0_LOW();
        break;

    case 1:
        U1_1A2_LOW();
        U1_1A1_LOW();
        U1_1A0_HIGH();
        break;

    case 2:
        U1_1A2_LOW();
        U1_1A1_HIGH();
        U1_1A0_LOW();
        break;

    case 3:
        U1_1A2_LOW();
        U1_1A1_HIGH();
        U1_1A0_HIGH();
        break;

    case 4:
        U1_1A2_HIGH();
        U1_1A1_LOW();
        U1_1A0_LOW();
        break;

    case 5:
        U1_1A2_HIGH();
        U1_1A1_LOW();
        U1_1A0_HIGH();
        break;

    case 6:
        U1_1A2_HIGH();
        U1_1A1_HIGH();
        U1_1A0_LOW();

    case 7:
        U1_1A2_HIGH();
        U1_1A1_HIGH();
        U1_1A0_HIGH();
        break;
    }
}


//==================================================
#define	U1_2A0_HIGH()			HAL_GPIO_WritePin(ADG1408_U1_2_A0_GPIO_Port, ADG1408_U1_2_A0_Pin, GPIO_PIN_SET)
#define	U1_2A0_LOW()			HAL_GPIO_WritePin(ADG1408_U1_2_A0_GPIO_Port, ADG1408_U1_2_A0_Pin, GPIO_PIN_RESET)

#define	U1_2A1_HIGH()			HAL_GPIO_WritePin(ADG1408_U1_2_A1_GPIO_Port, ADG1408_U1_2_A1_Pin, GPIO_PIN_SET)
#define	U1_2A1_LOW()			HAL_GPIO_WritePin(ADG1408_U1_2_A1_GPIO_Port, ADG1408_U1_2_A1_Pin, GPIO_PIN_RESET)

#define	U1_2A2_HIGH()			HAL_GPIO_WritePin(ADG1408_U1_2_A2_GPIO_Port, ADG1408_U1_2_A2_Pin, GPIO_PIN_SET)
#define	U1_2A2_LOW()			HAL_GPIO_WritePin(ADG1408_U1_2_A2_GPIO_Port, ADG1408_U1_2_A2_Pin, GPIO_PIN_RESET)


/****************************************************************************************************************************
* @Functions:U1_1_Channel()
* @Description:control the states the ADG1408 including U1_2. The ADG1408switches one of eight inputs to a common output as
	determined by the 3-bit binary address lines A0,A1 and A2.
* @Input:one of 6 inputs
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void U1_2_Channel(unsigned char sel)
{
    switch(sel)
    {
    case 0:
        U1_2A2_LOW();
        U1_2A1_LOW();
        U1_2A0_LOW();
        break;

    case 1:
        U1_2A2_LOW();
        U1_2A1_LOW();
        U1_2A0_HIGH();
        break;

    case 2:
        U1_2A2_LOW();
        U1_2A1_HIGH();
        U1_2A0_LOW();
        break;

    case 3:
        U1_2A2_LOW();
        U1_2A1_HIGH();
        U1_2A0_HIGH();
        break;

    case 4:
        U1_2A2_HIGH();
        U1_2A1_LOW();
        U1_2A0_LOW();
        break;

    case 5:
        U1_2A2_HIGH();
        U1_2A1_LOW();
        U1_2A0_HIGH();
        break;

    case 6:
        U1_2A2_HIGH();
        U1_2A1_HIGH();
        U1_2A0_LOW();
        break;

    case 7:
        U1_2A2_HIGH();
        U1_2A1_HIGH();
        U1_2A0_HIGH();
        break;
    }
}

/****************************************************************************************************************************
* @Functions:U2_1_Channel()
* @Description:control the states the ADG1408 including U2_1A,U2_1B,U2_1C,U2_3A,U2_3B,U2_3C and U1_1. The ADG1408switches one of
	eight inputs to a common output as determined by the 3-bit binary address lines A0,A1 and A2.
* @Input:pI2C:address of the UserExtPort
	channel:0~47
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char ExtAdcChannel1(unsigned char channel)
{
    if(channel > 47)	// swith to NC
    {
        U2_1_Channel(0);
        U1_1_Channel(6);
        return 0;
    }
    U2_1_Channel(channel%8);
    U1_1_Channel(channel/8);

    return 1;
}


/****************************************************************************************************************************
* @Functions:U2_1_Channel()
* @Description:control the states the ADG1408 including U2_2A,U2_2B,U2_2C,U2_4A,U2_4B,U2_4C and U1_2. The ADG1408switches one of
	eight inputs to a common output as determined by the 3-bit binary address lines A0,A1 and A2.
* @Input:pI2C:address of the UserExtPort
	channel:0~47
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char ExtAdcChannel2(unsigned char channel)
{
    if(channel > 47)	// switch to NC
    {
        U2_2_Channel(0);
        U1_2_Channel(6);
        return 0;
    }
    U2_2_Channel(channel%8);
    U1_2_Channel(channel/8);

    return 1;
}

